package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

//TODO:配置SqlSessionFactory和MapperScannerConfigurer
public class MybatisConfig {
    /*
               TODO:
                1. SqlSessionFactoryBean 是mybatis的spring整合包(mybatis-spring)提供的类,底层会提供 SqlSessionFactory
                    public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>{
                         public SqlSessionFactory getObject() throws Exception {
                                if (this.sqlSessionFactory == null) {
                                    this.afterPropertiesSet();
                                }
                                return this.sqlSessionFactory;
                            }
                    }
                2.当提供SqlSessionFactory工厂会话对象后就可以相当于设置mybatis-config.xml核心配置文件如下内容：
                    给javaBean起别名
                    <typeAliases>
                        <package name="com.itheima.domain"/>   ===> factoryBean.setTypeAliasesPackage("com.itheima.domain");
                    </typeAliases>
                    数据源：
                     <dataSource type="POOLED">
                        <property name="driver" value="${jdbc.driver}"></property>
                        <property name="url" value="${jdbc.url}"></property>
                        <property name="username" value="${jdbc.username}"></property>  ====》  factoryBean.setDataSource(ds);
                        <property name="password" value="${jdbc.password}"></property>
                     </dataSource>

            */
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource ds) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置pojo的包扫描
        factoryBean.setTypeAliasesPackage("com.itheima.pojo");
        //设置连接池
        factoryBean.setDataSource(ds);
        return factoryBean;
    }

    /*
        TODO:
            1.定义bean，返回MapperScannerConfigurer对象(mybatis的spring整合包提供的类),可以设置dao层的接口扫描
            完成的功能类似：
            <package name="com.itheima.dao"></package>
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置dao层的接口扫描
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }

}